/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formularios;

import Clases.AnchoColumna;
import Clases.AutoCompleteCombo;
import Clases.ComboBox;
import Clases.ModeloTabla;
import Clases.Tools;
import Clases.Transacciones;
import de.javasoft.swing.JYTable;
import de.javasoft.swing.JYTableHeader;
import de.javasoft.swing.JYTableScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.text.JTextComponent;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author Jrg
 */
public class inscripciones extends javax.swing.JInternalFrame {

    public static int IdMenu=2;
    JTable _tabla;
    ModeloTabla _modeloTabla;
    double _total;
    int _idAlumno=0;
    /**
     * Creates new form inscripciones
     */
    public inscripciones() 
    {
        initComponents();
        ObtenerFechaActual();
        CrearModeloTabla();
        CargarComboGrado();
        CargarComboAlumno();
        CargarComboArticulos();
        AgregarListenerComboAlumno();
    }
    
    
    private void ObtenerFechaActual()
    {
        jDateFecha.setDate(Transacciones.ObtenerFechaServidor(MainMenu.conexion));
        jDateDel.setDate(Transacciones.ObtenerFechaServidor(MainMenu.conexion));
        jDateAl.setDate(Transacciones.ObtenerFechaServidor(MainMenu.conexion));
        jDateVencimiento.setDate(Transacciones.ObtenerFechaServidor(MainMenu.conexion));
    }
    
    private void CargarComboGrado()
    {
        DefaultComboBoxModel modelo = Clases.ComboBox.CargarCombo("select idgrado id, nombre from grados where debaja is null order by idgrado asc", MainMenu.conexion);
        cbGrado.setModel(modelo);
        AutoCompleteDecorator.decorate(cbGrado);
    }
    
    private void CargarComboAlumno()
    {
        DefaultComboBoxModel modelo = Clases.ComboBox.CargarCombo("select idalumno id, nombre from alumnos where debaja is null order by nombre asc", MainMenu.conexion);
        cbAlumno.setModel(modelo);
        AutoCompleteDecorator.decorate(cbAlumno);
    }
    
    private void CargarComboArticulos()
    {
        DefaultComboBoxModel modelo = Clases.ComboBox.CargarCombo("select idarticulo id, nombre from articulos where debaja is null order by nombre asc", MainMenu.conexion);
        cbArticulos.setModel(modelo);
        AutoCompleteDecorator.decorate(cbArticulos);
    }
    
    private void AgregarListenerComboAlumno()
    {
        cbAlumno.addActionListener (new ActionListener () 
        {
            public void actionPerformed(ActionEvent e) 
            {
                try
                {
                    _idAlumno = ((ComboBox) cbAlumno.getSelectedItem()).getId();
                }
                catch(Exception ex){}
            }
        });
    }
    
    private void CrearModeloTabla()
    {
        try
        {
            /*columnas que no son editables*/
            Hashtable colReadOnly= new Hashtable();
            colReadOnly.clear();
            colReadOnly.put(0, 0); //IdServicio
            colReadOnly.put(1, 300); //Servicio
            colReadOnly.put(2, 75); //Monto
            colReadOnly.put(3, 200); //Vencimiento
            String[] columnNames = {"IdServicio","Servicio", "Monto","Vencimiento"};
            final Class<?>[] columnClasses = {Integer.class,String.class, Double.class, Date.class}; 
            _modeloTabla = new ModeloTabla(colReadOnly);
            _modeloTabla.setColumnIdentifiers(columnNames);
            _modeloTabla.RemoveAllRows();
            _tabla = new JTable(_modeloTabla)
            {
                @Override
                public Class<?> getColumnClass(int columnIndex)
                {
                    return columnClasses[columnIndex];
                }
            };
            AnchoColumna ac = new AnchoColumna(_tabla,colReadOnly);
//          OcultarColumnas oc = new OcultarColumnas(_tabla,0);
            JScrollPane scroll = new JScrollPane(_tabla);
            _tabla.getTableHeader().setReorderingAllowed(false);
            panelTabla.setVisible(false);
            panelTabla.add(scroll);
            panelTabla.setVisible(true);
            panelTabla.repaint();
        }
        catch(Exception ex)
        {
            
        }
    }
    
    private void AgregarServicio()
    {
        double monto=0;
        try
        {
            monto=(double)Math.round(Double.valueOf(tfMonto.getText())*100)/100;
        }
        catch(Exception ex)
        {
            
        }
        if (monto>0)
        {
            ComboBox servicio = ((ComboBox) cbArticulos.getSelectedItem());
            Add(servicio.getId(),servicio.getNombre(),monto,jDateVencimiento.getDate());
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Debe Ingresar un monto valido","Error",JOptionPane.ERROR_MESSAGE);
            tfMonto.setText("0.00");
        }
    }
    
    public void Add(int idservicio, String nombre, double monto, Date vencimiento)
    {
        Vector v= new Vector(6);            
        v.addElement(idservicio); //idservicio
        v.addElement(nombre); //nombre
        v.addElement(monto); //monto
        v.addElement(vencimiento); //fecha vencimiento
        _modeloTabla.addRow(v);
        _total +=monto;
        tfTotal.setText(String.valueOf(_total));
        tfTotal.repaint();
    }
    
    private void EliminarServicio()
    {
        int  _rowSeleccionada = _tabla.getSelectedRow();
        if(_rowSeleccionada!=-1)
        {
            int answer=JOptionPane.showConfirmDialog(null, "¿Desea eliminar la fila seleccionada?", "Confirme por favor", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(answer == JOptionPane.YES_OPTION)
            {
                double monto=(double)_tabla.getValueAt(_rowSeleccionada, 2);
                tfMonto.setText(String.valueOf(monto));
                cbArticulos.setSelectedItem(_tabla.getValueAt(_rowSeleccionada,0)); //selecciona el articulo en el combo
                jDateVencimiento.setDate((Date)_tabla.getValueAt(_rowSeleccionada,3));
                _modeloTabla.removeRow(_rowSeleccionada);
                _total -=monto;
                tfTotal.setText(String.valueOf(_total));
                tfTotal.repaint();
            }
        }
    }
    
    private void LimpiarVariables()
    {
        ObtenerFechaActual();
        this.cbGrado.setSelectedIndex(0);
        this.cbAlumno.setSelectedIndex(0);
        this.cbArticulos.setSelectedIndex(0);
        _idAlumno=0;
        _total=0;
        tfTotal.setText("0.00");
        tfMonto.setText("0.00");
        _modeloTabla.RemoveAllRows();

    }
    
    private void Nuevo()
    {
        int answer=JOptionPane.showConfirmDialog(null, "Perdera los datos no guardados, \n ¿desea continuar?", "Confirme por favor", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(answer == JOptionPane.YES_OPTION)
        {
            LimpiarVariables();
        }
    }
    
    private void Guardar() throws SQLException 
    {
        int answer=JOptionPane.showConfirmDialog(null, "¿Desea procesar la inscripción?", "Confirme por favor", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(answer == JOptionPane.YES_OPTION)
        {
            Connection _conexion=null;
            try
            {
                if (cbAlumno.getSelectedItem()!=null && cbGrado.getSelectedItem() !=null)
                {
                    int idInscripcion=0;
                    int idmovCuenta=0;
                    int idalumno =((ComboBox) cbAlumno.getSelectedItem()).getId();
                    int idgrado =((ComboBox) cbGrado.getSelectedItem()).getId();
                    java.sql.Date del=Tools.ObtenerFechaSql(jDateDel.getDate());
                    java.sql.Date al=Tools.ObtenerFechaSql(jDateAl.getDate());
                    if (!VerificarInscripcionExiste(idalumno,del,al))
                    {
                        if (ValidarDetalle())
                        {
                            Class.forName("com.mysql.jdbc.Driver").newInstance();
                            _conexion = DriverManager.getConnection("jdbc:mysql://"+MainMenu.server+"/"+MainMenu.dataBase,MainMenu.user, MainMenu.password);
                            _conexion.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
                            _conexion.setAutoCommit(false);//Inicia la transaccion
                            Statement st = _conexion.createStatement();
                            st.execute("insert into inscripciones (idalumno,idgrado,fecha,del,al,idusuario) values("+idalumno+","+idgrado+",'"+Tools.ObtenerFechaSql(jDateFecha.getDate())+
                                    "','"+del+"','"+al+"',"+MainMenu.IdUsuario+")",Statement.RETURN_GENERATED_KEYS);
                            ResultSet rs = st.getGeneratedKeys();
                            if(rs.next()) 
                                idInscripcion = rs.getInt(1);
                            for (int i = 0; i < _modeloTabla.getRowCount(); i++) 
                            {
                                PreparedStatement pst = _conexion.prepareStatement("insert into detalleinscripcion(idinscripcion,idarticulo,monto,saldo,vencimiento) VALUES (?,?,?,?,?)");
                                pst.setInt(1, idInscripcion);
                                pst.setInt(2, (int)_tabla.getValueAt(i, 0));
                                pst.setDouble(3, (double)_tabla.getValueAt(i, 2));
                                pst.setDouble(4, (double)_tabla.getValueAt(i, 2));
                                pst.setDate(5, Tools.ObtenerFechaSql((Date)_tabla.getValueAt(i, 3)));
                                pst.execute();

                            }
                            /*inserta el movimiento de cuenta */
                            Statement stMovCuenta = _conexion.createStatement();
                            stMovCuenta.execute("insert into movcuenta(fecha,tipo,id,idusuario,observaciones) values('"+Tools.ObtenerFechaSql(jDateFecha.getDate())+"',2,"+idInscripcion+","+MainMenu.IdUsuario+",'Inscripcion')",Statement.RETURN_GENERATED_KEYS);
                            ResultSet rsMovCuenta = stMovCuenta.getGeneratedKeys();
                            if(rsMovCuenta.next()) 
                                idmovCuenta = rsMovCuenta.getInt(1);
                            stMovCuenta.execute("insert into detallemovcuenta(idmovcuenta,idcuenta,debe,haber) values("+idmovCuenta+",41,"+0+","+_total+")");
                            stMovCuenta.execute("insert into detallemovcuenta(idmovcuenta,idcuenta,debe,haber) values("+idmovCuenta+",8,"+_total+","+0+")");
                            /*Actualizar el saldo de las 2 cuentas CAPITAL Y CUENTAS POR COBRAR */
                            st.execute("update cuentas set saldo = saldo + "+_total+" where idcuenta in (8,41)");
                            /*actualiza el grado grado del alumno */
                            st.execute("update alumnos set idgrado="+idgrado+" where idalumno="+idalumno);
                            _conexion.commit();
                            JOptionPane.showMessageDialog(this, "Los datos se almacenaron correctamente","Información",JOptionPane.INFORMATION_MESSAGE);
                            LimpiarVariables();
                        }
                        else
                            JOptionPane.showMessageDialog(this, "Debe agregar almenos un detalle a la inscripción","Error",JOptionPane.ERROR_MESSAGE);
                    }
                    else
                        JOptionPane.showMessageDialog(this, "Este alumno ya tiene una inscripción para este rango de fechas","Error",JOptionPane.ERROR_MESSAGE);
                }
                else
                    JOptionPane.showMessageDialog(this, "Debe seleccionar un alumno y grado","Error",JOptionPane.ERROR_MESSAGE);
            }
            catch(Exception ex)
            {
                if (_conexion != null) 
                    _conexion.rollback();
                JOptionPane.showMessageDialog(this, ex,"Error",JOptionPane.ERROR_MESSAGE);
            }
            finally
            {
                if (_conexion != null && !_conexion.isClosed())
                {
                    _conexion.close();
                    _conexion=null;
                    System.gc();
                }
            }
        }
    }
    
    private boolean VerificarInscripcionExiste(int idalumno,java.sql.Date del,java.sql.Date al)
    {
        try
        {
            ResultSet rs=null;
            rs=MainMenu.conexion.EjecutarConsulta("select count(*) from inscripciones where idalumno="+idalumno+" and debaja is null and (('"+del+"' between del and al) or ('"+al+"' between del and al))");
            if(rs.next())
            {
                if(rs.getInt(1)>0)
                {
                    return true;
                }
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex,"Error",JOptionPane.ERROR_MESSAGE);
        }
        finally
        {
            MainMenu.conexion.CerrarConexion();
        }
        return false;
    }
    
    private boolean ValidarDetalle()
    {
        if (_modeloTabla.getRowCount()>0)
            return true;
        return false;
    }
    
    private void AgregarCuotas()
    {
        ingresoCuotas d = new ingresoCuotas(null, true,this,jDateDel.getDate(),jDateAl.getDate(),_idAlumno);
        d.setVisible(true);         
        d.dispose();
        System.gc();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelEncabezado = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbAlumno = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbGrado = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jDateFecha = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jDateDel = new com.toedter.calendar.JDateChooser();
        jDateAl = new com.toedter.calendar.JDateChooser();
        btnIngresoCuotas = new javax.swing.JButton();
        panelAddServicio = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        cbArticulos = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        tfMonto = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jDateVencimiento = new com.toedter.calendar.JDateChooser();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        panelTabla = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        tfTotal = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Inscripcion");
        setToolTipText("");

        panelEncabezado.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Alumno");

        jLabel2.setText("Grado");

        jLabel3.setText("Fecha");

        jLabel4.setText("Del");

        jLabel5.setText("Al");

        btnIngresoCuotas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/pastfile24.png"))); // NOI18N
        btnIngresoCuotas.setText("Cuotas");
        btnIngresoCuotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresoCuotasActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelEncabezadoLayout = new org.jdesktop.layout.GroupLayout(panelEncabezado);
        panelEncabezado.setLayout(panelEncabezadoLayout);
        panelEncabezadoLayout.setHorizontalGroup(
            panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelEncabezadoLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, panelEncabezadoLayout.createSequentialGroup()
                        .add(jLabel2)
                        .add(18, 18, 18))
                    .add(panelEncabezadoLayout.createSequentialGroup()
                        .add(jLabel1)
                        .add(12, 12, 12)))
                .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(cbAlumno, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(cbGrado, 0, 162, Short.MAX_VALUE))
                .add(18, 18, 18)
                .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel4)
                    .add(jLabel5))
                .add(18, 18, 18)
                .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jDateDel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .add(jDateAl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(28, 28, 28)
                .add(jLabel3)
                .add(31, 31, 31)
                .add(jDateFecha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(btnIngresoCuotas)
                .addContainerGap())
        );
        panelEncabezadoLayout.setVerticalGroup(
            panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelEncabezadoLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelEncabezadoLayout.createSequentialGroup()
                        .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel1)
                                .add(cbAlumno, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel4))
                            .add(jDateDel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jDateFecha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3))
                        .add(19, 19, 19)
                        .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(panelEncabezadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel2)
                                .add(cbGrado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel5))
                            .add(jDateAl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(btnIngresoCuotas))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelAddServicio.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel6.setText("Servicio");

        jLabel7.setText("Monto");

        tfMonto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel8.setText("Vencimiento");

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add24.png"))); // NOI18N
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/delete24.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelAddServicioLayout = new org.jdesktop.layout.GroupLayout(panelAddServicio);
        panelAddServicio.setLayout(panelAddServicioLayout);
        panelAddServicioLayout.setHorizontalGroup(
            panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelAddServicioLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cbArticulos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 164, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel7)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(tfMonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(28, 28, 28)
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jDateVencimiento, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 156, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btnAgregar)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(btnEliminar)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        panelAddServicioLayout.setVerticalGroup(
            panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelAddServicioLayout.createSequentialGroup()
                .add(23, 23, 23)
                .add(panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jDateVencimiento, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel6)
                            .add(cbArticulos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel7)
                            .add(tfMonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8)))
                    .add(panelAddServicioLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btnAgregar)
                        .add(btnEliminar)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelTabla.setLayout(new java.awt.CardLayout());

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel9.setText("Total");

        tfTotal.setEditable(false);
        tfTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tfTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel9)
                .add(18, 18, 18)
                .add(tfTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 146, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 49, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel9))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add24.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/floppy24.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnNuevo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(btnGuardar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(btnNuevo)
                .add(18, 18, 18)
                .add(btnGuardar)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelEncabezado, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(panelAddServicio, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(panelTabla, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(panelEncabezado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(panelAddServicio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelTabla, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        AgregarServicio();
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try
        {
            Guardar();
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex,"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        EliminarServicio();
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        Nuevo();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnIngresoCuotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresoCuotasActionPerformed
         AgregarCuotas();
    }//GEN-LAST:event_btnIngresoCuotasActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnIngresoCuotas;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JComboBox cbAlumno;
    private javax.swing.JComboBox cbArticulos;
    private javax.swing.JComboBox cbGrado;
    private com.toedter.calendar.JDateChooser jDateAl;
    private com.toedter.calendar.JDateChooser jDateDel;
    private com.toedter.calendar.JDateChooser jDateFecha;
    private com.toedter.calendar.JDateChooser jDateVencimiento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel panelAddServicio;
    private javax.swing.JPanel panelEncabezado;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTextField tfMonto;
    private javax.swing.JTextField tfTotal;
    // End of variables declaration//GEN-END:variables
}
